<?php
require_once (DB_FILE);
require_once(USER_CONTROLLER_FILE);
require_once(ADMIN_CONTROLLER_FILE);

class GameTable {
    
    //  Return Array of [Op.Nick,Op.Avatar] by Player UID
    //  Looks in game table
    public static function getOpponentNickAndAvatar() {
        $my_uid = UserController::getUserID();
        $game_id = UserController::getGameID();
        $op_uid = 0;
        $op_nick = "";
        $op_avatar = 0;
        
        // Get Op. ID from Game Row
        $query = "SELECT *
               FROM games       
               WHERE id = $game_id
               LIMIT 1";   
        
        $db = new DBConnection();
        if ($db->executeQuery($query)) {
            $result = $db->getResult();
            if( ($result != null) && ($result->num_rows > 0) ) {
                // found game row
                $row = $result->fetch_assoc();
                if ( $row["p1_id"] == $my_uid) {
                    //p1_id is me!
                    //sanity check:
                    if ( $row["p2_id"] == $my_uid) {
                        // whos game is it?!
                        return null;                        
                    }
                    else {
                        $op_uid = $row["p2_id"];
                    }
                }
                else {
                    $op_uid = $row["p1_id"];
                }
            }
            else {
                //NOT FOUND
                return null;
            }
        }
        else {
            //DB ERROR
            return null;
        }
        
        // now we have op. uid
        // get op. nick and avatar
        $query = "SELECT *
               FROM users       
               WHERE id = $op_uid
               LIMIT 1";           
        
        if ($db->executeQuery($query)) {
            $result = $db->getResult();
            if( ($result != null) && ($result->num_rows > 0) ) {
                // found player row
                $row = $result->fetch_assoc();
                $op_nick = $row["nick"];
                $op_avatar = $row["avatar"];
                return array($op_nick, $op_avatar);
            }
            else {
                //player not found..?!
                return null;
            }
        }
        else {
            // DB Error
            return null;
        }
        
    }

    //  Return Array of [Op.Nick,Op.Avatar] from Users Table of the Op. by Player UID
    //  Looks in old game table
    public static function getOpponentNickAndAvatarFromOldGameTable() {
        $my_uid = UserController::getUserID();
        $game_id = UserController::getGameID();
        $op_uid = 0;
        $op_nick = "";
        $op_avatar = 0;
        
        // Get Op. ID from Game Row
        $query = "SELECT *
               FROM old_games       
               WHERE game_id = $game_id
               LIMIT 1";   
        
        $db = new DBConnection();
        if ($db->executeQuery($query)) {
            $result = $db->getResult();
            if( ($result != null) && ($result->num_rows > 0) ) {
                // found game row
                $row = $result->fetch_assoc();
                if ( $row["won"] == $my_uid) {
                    //p1_id is me!
                    //sanity check:
                    if ( $row["loose"] == $my_uid) {
                        // whos game is it?!
                        return null;                        
                    }
                    else {
                        $op_uid = $row["loose"];
                    }
                }
                else {
                    $op_uid = $row["won"];
                }
            }
            else {
                //NOT FOUND
                return null;
            }
        }
        else {
            //DB ERROR
            return null;
        }
        
        // now we have op. uid
        // get op. nick and avatar
        $query = "SELECT *
               FROM users       
               WHERE id = $op_uid
               LIMIT 1";           
        
        if ($db->executeQuery($query)) {
            $result = $db->getResult();
            if( ($result != null) && ($result->num_rows > 0) ) {
                // found player row
                $row = $result->fetch_assoc();
                $op_nick = $row["nick"];
                $op_avatar = $row["avatar"];
                return array($op_nick, $op_avatar);
            }
            else {
                //player not found..?!
                return null;
            }
        }
        else {
            // DB Error
            return null;
        }
        
    }
    
    // get Row From OldGames Table ny Game_ID 
    public static function getOldGameRow($game_id ) {

        $query = "SELECT * 
                    FROM old_games
                    WHERE game_id = ?";

        $db = new DBConnection();
        if ($db->executeBindedQuery($query,"i",$game_id)) {
            $result = $db->getResult();
            if( ( $result != null) && ($result->num_rows > 0) ) {
                $row = $result->fetch_assoc();
            }
            else {
                return null;
            }
        }
        else {
            return null;
        }
        return $row;
    }

    
    // Only By Admin!!!
    // return all active games rows - same structure as Games Table in DB
    public static function getListOfActiveGames() {
        //Sanity Check just to be safe...
        if ( !AdminController::isAdminLogin() ) {
            return null;
        }
        
        // Get All Games
        $query = "SELECT *
               FROM games";   
        
        $db = new DBConnection();
        if ($db->executeQuery($query)) {
            $result = $db->getResult();
            return $result;
        }
        else {
            return "DBERR" ;
        }
    }

    
    
    //return a user nike name and avatar by UID
    public static function getUserNickAndAvatar($uid) {
        $query = "SELECT nick, avatar 
                    FROM users
                    WHERE id = ?";

        $db = new DBConnection();
        if ($db->executeBindedQuery($query,"i",$uid)) {
            $result = $db->getResult();
            if( ( $result != null) && ($result->num_rows > 0) ) {
                $row = $result->fetch_assoc();
            }
            else {
                return null;
            }
        }
        else {
            return null;
        }
        $op_nick = $row["nick"];
        $op_avatar = $row["avatar"];
        return array($op_nick, $op_avatar);
    }
    
    
    
    //get the number of wins for this user
    public static function getUserWinsFromOldGames() {
        $my_uid = UserController::getUserID();
        
        $query = "SELECT count(won) as wins
            FROM old_games
            WHERE won = ?";

        $db = new DBConnection();
        if ($db->executeBindedQuery($query,"i",$my_uid)) {
            $result = $db->getResult();
            if( ( $result != null) && ($result->num_rows > 0) ) {
                $row = $result->fetch_assoc();
            }
            else {
                return null;
            }
        }
        else {
            return null;
        }
        $wins = $row["wins"];
        return $wins;
    }
    
    

    //get the number of looses for this user
    public static function getUserLoosesFromOldGames() {
        $my_uid = UserController::getUserID();
        
        $query = "SELECT count(lost) as looses
            FROM old_games
            WHERE lost = ?";

        $db = new DBConnection();
        if ($db->executeBindedQuery($query,"i",$my_uid)) {
            $result = $db->getResult();
            if( ( $result != null) && ($result->num_rows > 0) ) {
                $row = $result->fetch_assoc();
            }
            else {
                return null;
            }
        }
        else {
            return null;
        }
        $looses = $row["looses"];
        return $looses;
    }

    
    
    //get all old game data and opponent data as well
    public static function getUserLastWinsAndOpponentData() {
        $my_uid = UserController::getUserID();
        
        $query = "SELECT    old_games.duration as duration,
                            old_games.won_dmg as dmg,
                            users.nick as nick,
                            users.avatar as avatar,
                            old_games.date as date
                FROM old_games
                INNER JOIN users
                ON old_games.lost = users.id
                WHERE old_games.won=?
                ORDER BY old_games.date DESC
                LIMIT 5";

        $db = new DBConnection();
        if ($db->executeBindedQuery($query,"i",$my_uid)) {
            $result = $db->getResult();
            if( ( $result != null) && ($result->num_rows > 0) ) {
                return $result;
            }
            else {
                return null;
            }
        }
        else {
            return null;
        }
    }
    

    

    
    public static function getUserLastLoosesAndOpponentData() {
        $my_uid = UserController::getUserID();
        
        $query = "SELECT    old_games.duration as duration,
                            old_games.won_dmg as dmg,
                            users.nick as nick,
                            users.avatar as avatar,
                            old_games.date as date
                FROM old_games
                INNER JOIN users
                ON old_games.won = users.id
                WHERE old_games.lost=?
                ORDER BY old_games.date DESC
                LIMIT 5";

        $db = new DBConnection();
        if ($db->executeBindedQuery($query,"i",$my_uid)) {
            $result = $db->getResult();
            if( ( $result != null) && ($result->num_rows > 0) ) {
                return $result;
            }
            else {
                return null;
            }
        }
        else {
            return null;
        }
    }
    
    public static function getLastPlayedGamesAndAvatars() {

        $query = "SELECT    old_games.duration as duration,
                            old_games.moves as moves,
                            old_games.date as date,
                            u1.avatar AS win_avatar,
                            u1.nick AS win_nick,
                            u2.avatar AS lost_avatar,
                            u2.nick AS lost_nick
            FROM old_games, users AS u1, users AS u2
            WHERE old_games.won = u1.id AND old_games.lost = u2.id
            ORDER BY old_games.date DESC
            LIMIT 10;";

        $db = new DBConnection();
        if ($db->executeQuery($query)) {
            $result = $db->getResult();
            if( ( $result != null) && ($result->num_rows > 0) ) {
                return $result;
            }
            else {
                return null;
            }
        }
        else {
            return null;
        }
    }
    
    
}


